using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ball_control : MonoBehaviour
{
    private Transform m_transform;//摄像机父物体Transform
    private Rigidbody rigid_body;
    public float jump_speed=0.5f;
    public float acc = 0.01f;

    private bool isLanded = false;
    private void Start()
    {
        m_transform = GetComponent<Transform>();
        rigid_body = GetComponent<Rigidbody>();
    }
    private void Update()
    {
        Control();
    }

    /// OnCollisionEnter is called when this collider/rigidbody has begun
    /// touching another rigidbody/collider.
    /// </summary>
    /// <param name="other">The Collision data associated with this collision.</param>
    void OnCollisionEnter(Collision other)
    {
        if( other.gameObject.tag == "ground")
            this.isLanded=true;
    }

    void OnBecameInvisible()
    {
        Destroy(this.gameObject);  //删除游戏对象
    }
    void Control()
    {
        
        //按键盘W向上移动
        if (Input.GetKey(KeyCode.W))
        {
            rigid_body.velocity += Vector3.forward * acc;
        }
        else if (Input.GetKey(KeyCode.S))//按键盘S向下移动
        {
            rigid_body.velocity -= Vector3.forward * acc;
        }
        
        if (Input.GetKey(KeyCode.A))//按键盘A向左移动
        {
            rigid_body.velocity -= Vector3.right * acc;
        }
        else if (Input.GetKey(KeyCode.D))//按键盘D向右移动
        {
            rigid_body.velocity += Vector3.right * acc;
        }
        if(Input.GetKey(KeyCode.Space))
        {
            if(isLanded==true)
            {
                rigid_body.velocity += Vector3.up * jump_speed;
                isLanded = false;
            }
        }
    }
}